package com.xique.door.controller;


import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.api.request.build.RemoteBuildingListRequest;
import com.xique.door.bean.domain.AccessDevice;
import com.xique.door.bean.domain.BuildAreaV1;
import com.xique.door.bean.domain.BuildBuilding;
import com.xique.door.bean.request.buildBuildng.BuildBuildingAddRequest;
import com.xique.door.bean.request.buildBuildng.BuildBuildingDeleteRequest;
import com.xique.door.bean.request.buildBuildng.BuildBuildingEditRequest;
import com.xique.door.bean.request.buildBuildng.BuildBuildingListRequest;
import com.xique.door.biz.service.IBuildBuildingBizService;
import com.xique.door.service.IBuildAreaService;
import com.xique.door.service.IBuildBuildingService;
import com.xique.system.api.RemoteSystemUserService;
import com.xique.system.api.dto.OperateDTO;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 楼栋Controller
 *
 * @author shd
 * @date 2021-09-10
 */
@RestController
@RequestMapping("/community/building")
public class BuildBuildingController extends BaseController {

    @Autowired
    private IBuildBuildingService buildBuildingService;

    @Autowired
    private IBuildBuildingBizService buildBuildingBizService;


    @Autowired
    private IBuildAreaService areaService;


    @Autowired
    private RemoteSystemUserService remoteSystemUserService;


    /**
     * 查询楼栋列表
     */
    @PreAuthorize(hasPermi = "door:building:list")
    @GetMapping("/list")
    public TableDataInfo list(BuildBuilding buildBuilding) {
        startPage();
        buildBuilding.setParkId(SecurityUtils.getParkId());
        List<BuildBuilding> list = buildBuildingService.selectBuildBuildingList(buildBuilding);
        if (CollectionUtils.isEmpty(list)) {
            return getDataTable(list);
        }
        List<Long> areaIds = list.stream().map(BuildBuilding::getAreaId).collect(Collectors.toList());
        List<BuildAreaV1> areas = areaService.listByIds(areaIds);
        for (BuildBuilding building : list) {
            BuildAreaV1 buildAreaV1 = areas.stream().filter(e -> building.getAreaId().equals(e.getId())).findFirst().get();
            building.setAreaName(buildAreaV1.getAreaName());
        }
        List<String> userIds = list.stream().filter(e -> StringUtils.isNotEmpty(e.getUpdateBy())).map(e -> e.getUpdateBy()).collect(Collectors.toList());
        if (!userIds.isEmpty()) {
            R<List<OperateDTO>> operateList = remoteSystemUserService.getOperateList(StringUtils.join(userIds, ","));
            List<OperateDTO> data = operateList.getData();
            list.forEach(item -> {
                OperateDTO operateDTO = data.stream().filter(e -> String.valueOf(e.getOperateId()).equals(item.getUpdateBy())).findAny().orElse(null);
                if (operateDTO != null) {
                    item.setUpdateBy(operateDTO.getOperateName());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出楼栋列表
     */
    @PreAuthorize(hasPermi = "door:building:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuildBuilding buildBuilding) throws IOException {
        List<BuildBuilding> list = buildBuildingService.selectBuildBuildingList(buildBuilding);
        ExcelUtil<BuildBuilding> util = new ExcelUtil<BuildBuilding>(BuildBuilding.class);
        util.exportExcel(response, list, "building");
    }

    /**
     * 新增保存楼栋
     */
    @PreAuthorize(hasPermi = "door:building:add")
    @Log(title = "楼栋", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("楼栋添加")
    public AjaxResult addSave(@RequestBody BuildBuildingAddRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return buildBuildingBizService.addBuilding(request);
    }

    /**
     * 新增保存楼栋
     */
    @PreAuthorize(hasPermi = "door:building:add")
    @Log(title = "楼栋", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    @ApiOperation("楼栋添加")
    public AjaxResult batchAdd(@RequestBody BuildBuildingAddRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return buildBuildingBizService.batchAddBuilding(request);
    }

    /**
     * 修改保存楼栋
     */
    @PreAuthorize(hasPermi = "door:building:edit")
    @Log(title = "楼栋", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editSave(@RequestBody BuildBuildingEditRequest request) {
        return buildBuildingBizService.buildingEdit(request);
    }

    /**
     * 删除楼栋
     */
    @PreAuthorize(hasPermi = "door:building:remove")
    @Log(title = "楼栋", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(BuildBuildingDeleteRequest request) {
        return buildBuildingBizService.buildingDelete(request);
    }

    /**
     * 查询楼栋详情
     */
    @PreAuthorize(hasPermi = "door:building:query")
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("id") Long id) {
        BuildBuilding buildBuilding = buildBuildingBizService.selectBuildBuildingById(id);
        return AjaxResult.success(buildBuilding);
    }

    /**
     * 查询楼栋详情
     */
    @PreAuthorize(hasPermi = "door:building:query")
    @GetMapping("/devices")
    public AjaxResult selectBuildingDevices(@RequestParam("id") Long id) {

        return AjaxResult.success(buildBuildingBizService.selectBuildingDevices(id));
    }

    @PostMapping("/openApi/v1/list")
    public AjaxResult buildingListV1(@RequestBody RemoteBuildingListRequest request) {
        return buildBuildingBizService.selectBuildingList(request);
    }

    @PreAuthorize(hasPermi = "door:building:list")
    @GetMapping("/buildingList")
    public AjaxResult buildingList(BuildBuildingListRequest request) {
        return buildBuildingBizService.getBuildingList(request);
    }

   /**
    * 删除楼栋与设备的关系
    */

   @PostMapping("/delBuildingDevices")
   public AjaxResult delBuildingDevices(@RequestParam("ids") List<Long> ids) {

       return AjaxResult.success(buildBuildingBizService.delBuildingDevices(ids));
   }

   /**
    * 添加楼栋设备
    */
   @PostMapping("/addBuildingDevices")
   public AjaxResult addBuildingDevices(@RequestParam("ids") List<Long> ids, @RequestParam("buildBuildingId") Long buildBuildingId) {

       return AjaxResult.success(buildBuildingBizService.addBuildingDevices(ids, buildBuildingId));
   }

   /**
    * 未授权的设备
    */

   @GetMapping("/getBuildingDevices")
   public AjaxResult getBuildingDevices(Long buildBuildingId) {

       return buildBuildingBizService.getBuildingDevices(buildBuildingId);
   }
}
